var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery;

	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
});

/*------------单位节点树.start------------*/
function showUnitTree() {
	$('#applyUnitTree').show();
}

var company;

function getAllCompany() {
	$.ajax({
		url: url + 'baseEnt/getAllEnt',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {},
		success: function(data) {
			$('#applyUnitTree').empty();
			if(data.code == 0) {
				company = data.data;
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
}
getAllCompany();

function getAllFrequency() {
	$.ajax({
		url: url + 'dangerList/getAllFrequency',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			Token: salt
		},
		success: function(data) {
			$('#frequencyId').empty();
			if(data.code == 0) {
				var list = data.data;
				var opt = '<option value="0">请选择</option>'
				for(var i = 0; i < list.length; i++) {
					opt += '<option value="' + list[i].name + '" >' + list[i].name + '</option>'
				}
				$('#frequencyId').append(opt);
                layui.use(['form'], function () {
                    var form = layui.form
                    form.render();
                })
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
}
getAllFrequency();

var setting = {
	data: {
		simpleData: {
			enable: true
		}
	},
	check: {
		enable: true,
		chkStyle: "radio", //单选框
		radioType: "all" //对所有节点设置单选
	},
	callback: {
		onCheck: zTreeOnCheck
	}
};

$.fn.zTree.init($("#applyUnitTree"), setting, company);

function zTreeOnCheck(event, treeId, treeNode) {
	var checked = treeNode.checked;
	if(checked) {
		$('#applyUnit').val(treeNode.name);
	} else {
		$('#applyUnit').val("");
	}
	$('#applyUnitTree').hide();
};

function getTreeVal() {
	var unitTreeVal = [];
	var treeObj = $.fn.zTree.getZTreeObj("applyUnitTree");
	var nodes = treeObj.getCheckedNodes(true);
	for(var i = 0; i < nodes.length; i++) {
		unitTreeVal.push(nodes[i].id); //获取选中节点的值
	}
	return unitTreeVal;
}
/*------------单位节点树.end------------*/
function addInfo() {
	name = $.trim($("#name").val()); //检查表名称
	if(name == "") {
		layer.msg('请填写名称');
		return;
	}
	var applyUnitId = getTreeVal().toString(); //适用单位id
	if(applyUnitId == 0 || applyUnitId == "") {
		if(checkId != false) { //编辑
			applyUnitId = editCompanyId;
		} else {
			layer.msg('请选择适用单位');
			return;
		}
	}
	if(checkId == false) { //新增时
		checkId = "";
	}
	var postUrl;

	if($('#id').val() == '') {
		postUrl = url + 'dangerList/addERR'
	} else {
		postUrl = url + 'dangerList/editERR'
	}

	$.ajax({
		url: postUrl,
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			id: $('#id').val(),
			Token: salt,
			type: $("#type").val(),
			name: $("#name").val(),
			seat: $("#seat").val(),
			number: $("#number").val(),
			features: $("#features option:selected").val(),
			entId: applyUnitId,
			content: $("#content").val(),
			frequencyId: $("#frequencyId option:selected").val(),
			staff: $("#staff").val(),
			remarks: $("#remarks").val()
		},
		success: function(data) {
			if(data.code == 0) {
				var str = "新增危险源清单提交成功！";
				if(checkId != "") {
					str = "修改危险源清单提交成功！";
				}
				layer.confirm(str, {
					btn: ['确定'] //按钮
				}, function(index) {
					layer.close(index);
					location.href = "../list/list.html";
				});
			} else {
				layer.msg(data.msg);
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
}
/*-------------编辑获取值.start-------------*/
var checkId = getQueryVariable("id");
var editCompanyId;
if(checkId != false) {
	$("#pageTitle").html("编辑危险源清单");
	$.ajax({
		url: url + 'dangerList/getInfo',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			Token: salt,
			id: checkId,
		},
		success: function(data) {
			if(data.code == 0) {
				initEditItem(data.data);
			} else {
				setTimeout(function() {
					layer.msg(data.msg);
				}, 1000);
				checkId = false;
				initItemShow();
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
} else {
	$("#pageTitle").html("新增危险源清单");
}

function initEditItem(info) {
	$('#id').val(checkId);
	$("#name").val(info.name);
	$("#seat").val(info.seat);
	$("#matterType").val(info.matterType);
	$("#applyUnit").val(info.entName);
    $("#type").val(info.type);
	$("#number").val(info.numberNo);
	$('#features').find("option:contains('" + info.features + "')").attr("selected", true);
	$("#content").val(info.content);
	$('#frequencyId').find("option:contains('" + info.frequencyId + "')").attr("selected", true);
	$("#staff").val(info.staff);
	$("#remarks").val(info.remarks);
	editCompanyId = info.entId;
}
/*-------------编辑获取值.end-------------*/